import config from './config.js'
import serve from './http.js'
import axios from 'axios'
import store from '../store'

export function fileUploadPath () {
  return config.baseURL + 'file/upload'
}

export function httpHost () {
  return config.host
}

export function httpGet (url, param) {
  if (url) {
    config.url = url
  }
  if (param) {
    config.params = param
  }
  const token = store.getters.getAccessToken
  if (token) {
    config.headers.Authorization = 'bearer ' + token
  }
  config.data = null
  config.method = 'GET'
  console.log(config)
  console.log(process.env.API_HOST)

  return serve.request(config)
}

export function httpDownload (url, param, data) {
  if (param) {
    return axios.get(url, {
      baseURL: config.baseURL,
      params: param,
      responseType: 'arraybuffer'
    })
  }
  if (data) {
    return axios.post(url, {
      baseURL: config.baseURL,
      data: data,
      responseType: 'arraybuffer'
    })
  }
}
// config 版本
export function httpPost (url, data) {
  const conf = {
    baseURL: config.baseURL,
    headers: {}
  }
  const token = store.getters.getAccessToken
  if (token) {
    conf.headers.Authorization = 'bearer ' + token
  }
  conf.url = url
  conf.params = null
  conf.data = data
  return serve.post(url, data, conf) // axios with interpeter
}

export function httpPut (url, data) {
  const conf = {
    baseURL: config.baseURL,
    headers: {
      'Authorization': config.authorizationValue
    }
  }
  const token = store.getters.getAccessToken
  if (token) {
    conf.headers.Authorization = 'bearer ' + token
  }
  return serve.put(url, data, conf)
}

export function httpDel (url, data) {
  const conf = {
    baseURL: config.baseURL,
    headers: {}
  }
  const token = store.getters.getAccessToken
  if (token) {
    conf.headers.Authorization = 'bearer ' + token
  }
  conf.params = data
  conf.url = url
  conf.method = 'DELETE'
  return serve.request(conf)
}

export function httpLogin (url, data) {
  const conf = {
    baseURL: config.baseURL,
    headers: {
      'Authorization': config.authorizationValue
    }
  }
  return serve.post(url, data, conf)
}

/** how to user axios
 axios#request(config)
 axios#get(url[, config])
 axios#deconste(url[, config])
 axios#head(url[, config])
 axios#post(url[, data[, config]])
 axios#put(url[, data[, config]])
 axios#patch(url[, data[, config]])
 */
